﻿/// <reference name="MicrosoftAjax.js" />
// Register the namespace for the control.
Type.registerNamespace('Medianamik');

Medianamik.ImagePicker = function Medianamik$ImagePicker(element) {
    Medianamik.ImagePicker.initializeBase(this, [element]);

    this._imageContainer = null;
    this._hiddenPathField = null;
    this._opener = null;
    this._spacer = null;

}

function Medianamik$ImagePicker$initialize() {
    Medianamik.ImagePicker.callBaseMethod(this, 'initialize');
}

function Medianamik$ImagePicker$dispose() {
    Medianamik.ImagePicker.callBaseMethod(this, 'dispose');
}

function Medianamik$ImagePicker$get_imageContainer() {
    return this._imageContainer;
}

function Medianamik$ImagePicker$set_imageContainer(element) {
    this._imageContainer = element;
}

function Medianamik$ImagePicker$get_hiddenPathField() {
    return this._hiddenPathField;
}

function Medianamik$ImagePicker$set_hiddenPathField(element) {
    this._hiddenPathField = element;
}

function Medianamik$ImagePicker$get_opener() {
    return this._opener;
}

function Medianamik$ImagePicker$set_opener(component) {
    this._opener = component;
}

function Medianamik$ImagePicker$get_spacer() {
    return this._spacer;
}

function Medianamik$ImagePicker$set_spacer(value) {
    this._spacer = value;
}

function Medianamik$ImagePicker$imageManagerCallBack(sender, args) {
    if(args){ /* Args est undefined si on Cancel */
        var selectedImg = args.get_value();
        var r = new RegExp("^https?:\/\/[^/]+(.*)$");
        var result = r.exec(selectedImg.src);
        var path = selectedImg.src;
        if (result.length > 1) {
            path = result[1];
        }
        var img = $(this._imageContainer);
        img.css('background-image', 'url(' + path + ')');
        this._hiddenPathField.value = path;
    }
}

function Medianamik$ImagePicker$reset() {

    $(this._imageContainer).css('background-image','url(' + this.get_spacer() + ')');
    this._hiddenPathField.value = '';
}



Medianamik.ImagePicker.prototype = {
    initialize: Medianamik$ImagePicker$initialize,
    dispose: Medianamik$ImagePicker$dispose,
    get_imageContainer: Medianamik$ImagePicker$get_imageContainer,
    set_imageContainer: Medianamik$ImagePicker$set_imageContainer,
    get_hiddenPathField: Medianamik$ImagePicker$get_hiddenPathField,
    set_hiddenPathField: Medianamik$ImagePicker$set_hiddenPathField,
    imageManagerCallBack: Medianamik$ImagePicker$imageManagerCallBack,
    get_opener: Medianamik$ImagePicker$get_opener,
    set_opener: Medianamik$ImagePicker$set_opener,
    get_spacer: Medianamik$ImagePicker$get_spacer,
    set_spacer: Medianamik$ImagePicker$set_spacer,
    reset: Medianamik$ImagePicker$reset
}

// Register the class as a type that inherits from Sys.UI.Control.
Medianamik.ImagePicker.registerClass('Medianamik.ImagePicker', Sys.UI.Control);

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();